import Vue from 'vue'
import VueRouter from 'vue-router'
import homeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "homeView"
  },
  {
    path: '/homeView',
    name: 'homeView',
    component: homeView
  },
  {
    path: '/detailsView',
    name: 'detailsView',
    component: () => import('../views/detailsView.vue'),

    beforeEnter: (to, from, next) => {
      let token = window.sessionStorage.getItem("token");
      if (!token) {
        next("/signinView");
        return;
      } else {
        next();
      }
    }
  },
  {
    path: '/shopCarView',
    name: 'shopCarView',
    component: () => import('../views/shopCarView.vue'),

    beforeEnter: (to, from, next) => {
      let token = window.sessionStorage.getItem("token");
      if (!token) {
        next("/signinView");
        return;
      } else {
        next();
      }
    }
  },
  {
    path: '/paymentView',
    name: 'paymentView',
    component: () => import('../views/paymentView.vue')
  },
  {
    path: '/signinView',
    name: 'signinView',
    component: () => import('../views/signinView.vue')
  },
  {
    path: '/registerView',
    name: 'registerView',
    component: () => import('../views/registerView.vue')
  },
  {
    path: '/myView',
    name: 'myView',
    component: () => import('../views/myView.vue')
  },
  {
    path: '/searchView',
    name: 'searchView',
    component: () => import('../views/searchView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
